package com.qf.xiaomi.dao.impl;

import com.qf.xiaomi.dao.orderDao;
import com.qf.xiaomi.pojo.TbOrder;
import com.qf.xiaomi.pojo.TbOrderdetail;
import com.qf.xiaomi.utils.DButils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by 田贵宾 on 2022/8/20 9:49
 *
 * @author 田贵宾
 * @version 1.0
 * //TODO java练习阶段
 * @since 1.0
 */
public class orderDaoImpl implements orderDao {
    QueryRunner queryRunner = new QueryRunner(DButils.getdataSource());
    @Override
    public List<TbOrder> selectAll(long uid) {
        String sql="select * from tb_order where uid=?";
        try {
            List<TbOrder> tbOrders = queryRunner.query(sql, new BeanListHandler<>(TbOrder.class), uid);
            return tbOrders;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void createOrder(String oid, long aid, long uid, long sum) {
        String sql = "insert into tb_order (id,uid,money,aid) values(?,?,?,?)";
        try {
            int update = queryRunner.update(sql, oid, uid, sum, aid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void orderDetail(String oid, Long kzid, long num, long money) {
        String sql = "insert into tb_orderdetail values(null,?,?,?,?)";
        try {
            int update = queryRunner.update(sql, oid, kzid, num, money);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<TbOrder> selectOrderByUid(long uid) {
        String sql = "select * from tb_order where uid=?";
        try {
            List<TbOrder> list = queryRunner.query(sql, new BeanListHandler<>(TbOrder.class), uid);
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void updateStatus(String oid, String status) {
        String sql = "update tb_order set status=? where id=?";
        try {
            int update = queryRunner.update(sql, status, oid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public TbOrder findByOid(String oid) {
        String sql = "select * from tb_order where id=?";
        try {
            TbOrder order = queryRunner.query(sql, new BeanHandler<>(TbOrder.class), oid);
            return order;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<TbOrderdetail> fingByOid(String oid) {
        String sql = "select * from tb_orderdetail where oid=?";
        try {
           List<TbOrderdetail>  detail = queryRunner.query(sql, new BeanListHandler<>(TbOrderdetail.class), oid);
            return  detail;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
